package me.zhengjie.minbearwcs.dao.agv.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.minbearwcs.entity.PO.agv.missionEnum.AgvMissionStatus;
import me.zhengjie.minbearwcs.entity.PO.wcs.mission.type.MissionType;
import org.springframework.stereotype.Repository;
import me.zhengjie.minbearwcs.dao.agv.inf.AgvMissionDao;
import me.zhengjie.minbearwcs.entity.Mapper.agv.AgvMissionMapper;
import me.zhengjie.minbearwcs.entity.PO.agv.AgvMission;

import java.time.LocalDateTime;
import java.util.Optional;

@Repository
@RequiredArgsConstructor
public class AgvMissionDaoImpl implements AgvMissionDao {

    private final AgvMissionMapper mapper;

    @Override
    public boolean insert(AgvMission agvMission) {
        return mapper.insert(agvMission) == 1;
    }

    @Override
    public AgvMission selectByTaskId(String taskId) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(AgvMission::getTaskId, taskId)
                .one();
    }

    @Override
    public boolean updateAgvMissionBegin(String taskId, AgvMissionStatus status, int completeNum, String agvId, LocalDateTime startTime) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(AgvMission::getTaskId, taskId)
                .set(AgvMission::getTaskStatus, status)
                .set(AgvMission::getCompleteActionNum, completeNum)
                .set(AgvMission::getAgvId, agvId)
                .set(AgvMission::getStartTime, startTime)
                .update();
    }

    @Override
    public MissionType selectWcsMissionTypeByTaskId(String taskId) {
        return MissionType.valueOf(
                new LambdaQueryChainWrapper<>(mapper)
                        .eq(AgvMission::getTaskId, taskId)
                        .select(AgvMission::getWcsMissionType)
                        .one()
                        .getWcsMissionType()
        );
    }

    @Override
    public boolean updateAgvMissionFinish(String taskId, AgvMissionStatus status, int completeNum, LocalDateTime finishTime) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(AgvMission::getTaskId, taskId)
                .set(AgvMission::getTaskStatus, status)
                .set(AgvMission::getCompleteActionNum, completeNum)
                .set(AgvMission::getFinishTime, finishTime)
                .update();
    }

    @Override
    public boolean deleteById(int id) {
        return mapper.deleteById(id) == 1;
    }

    @Override
    public AgvMission selectByPallet(String palletCode) {
        Optional<AgvMission> opt = new LambdaQueryChainWrapper<>(mapper)
                .eq(AgvMission::getPalletCode, palletCode)
                .oneOpt();

        return opt.orElse(null);
    }

    @Override
    public boolean judgePalletHasAgvMission(String pallet) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(AgvMission::getPalletCode, pallet)
                .count() > 0;
    }


}
